/**
 * e-fuli.com Inc.
 * Copyright (c) 2015-2018 All Rights Reserved.
 */
package efuliBusiness.ebuyOpenPlatformUtil;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * <pre>
 *
 * </pre>
 *
 * @author jiangjin
 * @version $Id: AlipayVoucherTest.java, v 0.1 2018年3月8日 下午4:12:36 jiangjin Exp $
 */
public class OpenPlatformHttp {
    //日志对象
//    MyFile myFile = new MyFile();

    /**需要修改的部分*/
    private static String appId = "welfarepoint12345678901234567890";   //福员外
//    private static String appId = "9d1204f8211a424aa881ed838e3895b4";   //平安一账通
//    private static String appId = "38ecf881d0374b0aaa6eab4717a07b32";   //聚杰
    private static String version = "20171227";
    
    /**需要修改的部分*/
    private static String appPrivateKey;
    private static String publicKey;
    private static final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    
    static {
        appPrivateKey = Keys.RSA_PRIVATE_key;
        publicKey = Keys.PUBLIC_key;
    }

    public String sendHttp(String app_host, JSONObject jsonParam, String action){
        String timestamp = String.valueOf(System.currentTimeMillis());
//        jsonParam.put("request_id", timestamp);

        try {
            String base64String = Base64Util.encodeBase64ToString(jsonParam.toJSONString());
            Map<String, String> requestHeader = new HashMap<String, String>();
            requestHeader.put("app_id", appId);
            requestHeader.put("action", action);
//            requestHeader.put("msign", MD5Util.md5(timestamp + "efuli@2099"));
            requestHeader.put("biz_content", base64String);
            requestHeader.put("timestamp", timestamp);
            requestHeader.put("version", version);
            // 签名前字符串
            String signStr = SignatureUtil.getSignContent(requestHeader);
//            System.out.println(app_host);
//            System.out.println("request param: " + jsonParam.toJSONString());
            // 签名
            byte[] signed = SignatureUtil.sign(SignatureUtil.str2PrivateKey(appPrivateKey), signStr);
            String sign = new String(Base64Util.encodeBase64(signed), "UTF-8");
            // 验证签名
            boolean check = SignatureUtil.verifySign(SignatureUtil.str2PublicKey(publicKey), signStr, signed);
            requestHeader.put("sign", sign);
            HttpPost httpPost = new HttpPost(app_host);
            StringEntity entity = new StringEntity(JSONObject.toJSONString(requestHeader),"utf-8");//解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            HttpEntity entityResult = response.getEntity();
            String res = EntityUtils.toString(entityResult, "utf-8");
            System.out.println("-------- " + res);
            JSONObject jsonRes = JSONObject.parseObject(res);
//            System.out.println("--debug-- " + jsonRes.toJSONString());
            String resContent = jsonRes.getString("biz_content");
//            System.out.println("---- " + Base64Util.decodeBase64ToString(resContent));
//            myFile.writeLog("来吧: " + Base64Util.decodeBase64ToString(resContent) + "\n");
            return Base64Util.decodeBase64ToString(resContent);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
